db "GXOS"
org 07c00h
	jmp start
	LBA dword 0
	GDT equ 0500h
	inode equ 0900h
	dir equ 04900h
	loader db "loader"
start:	mov ax,cs
	mov ds,ax
	mov es,ax
	mov ss,ax
	;GDT 0x500h
	mov bx,GDT
	mov ax,2
	call hd_read

	mov bx,0508h
	mov ax,[bx]
	mov bx,inode
	mov cx,010h
inode_table:
	call hd_read
	inc ax
	loop inode_table

	mov ax,[inode+128+40]
	mov bx,dir
	call hd_read

	mov bx,dir
loader_len:
	mov al,[bx+6]
	cmp al,6
	jz boot_dir_name
	cmp bx,dir+0400h
	jz no_l
	mov ax,[bx+4]
	add bx,ax
	jmp loader_len
boot_dir_name:
	mov al,[bx+8]
	cmp al,'L'
	jnz no_find
	mov al,[bx+9]
	
no_find:
	mov ax,[bx+4]
	add bx,ax
	jmp loader_len
	;Loader.bin

	jmp $

str:	db 'No Loader!'
no_l:	mov si,str
	mov di,08b000h
	mov cx,10
	mov ah,0ch
re:	mov al,[si]
	mov [di],ax
	inc si
	inc di
	inc di
	loop re
	jmp $
hd_read:
	push cx
	push bx
	push bx
	push ax
        mov dx,01f7h
ready:
	
        in al,dx
        and al,080h
        cmp al,080h
        jz ready

        mov al,2
        mov dx,01f2h
        out dx,al
        pop ax
        shl ax,1

        inc dx
        out dx,al
        shr ax,8
	inc dx
	out dx,al
	mov ax,0e000h
	inc dx
	out dx,al
	shr ax,8
	inc dx
	out dx,al
	
        mov al,020h
        inc dx
        out dx,al
check:
        in al,dx
        and al,088h
        cmp al,08h
        jnz check

        pop bx
        mov cx,512
        mov dx,01f0h
read:
        in ax,dx
        mov [ds:bx],ax
        add bx,2
        loop read
	pop bx
	pop cx
        ret

times 510-($-$$) db 0
dw 0xaa55
